<template>
  <!-- 列表 -->
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="航班号" align="center" prop="flightNo" />
        <el-table-column label="商务舱加餐数量" align="center" prop="bussClassQty" />
        <el-table-column label="高级经济舱加餐数量" align="center" prop="premEcoClassQty" />
        <el-table-column label="经济舱加餐数量" align="center" prop="ecoClassQty" />
        <el-table-column label="机组加餐数量" align="center" prop="crewQty" />
        <el-table-column label="日期" align="center" prop="recordDate" min-width="120"/>
        <!--<el-table-column-->
        <!--  label="创建时间"-->
        <!--  align="center"-->
        <!--  prop="createTime"-->
        <!--  :formatter="dateFormatter"-->
        <!--  width="180px"-->
        <!--/>-->
        <!--<el-table-column label="操作" align="center" min-width="120px">-->
        <!--  <template #default="scope">-->
        <!--    <el-button-->
        <!--      link-->
        <!--      type="primary"-->
        <!--      @click="openForm('update', scope.row.id)"-->
        <!--      v-hasPermi="['pplan:recipe-add-record:update']"-->
        <!--    >-->
        <!--      编辑-->
        <!--    </el-button>-->
        <!--    <el-button-->
        <!--      link-->
        <!--      type="danger"-->
        <!--      @click="handleDelete(scope.row.id)"-->
        <!--      v-hasPermi="['pplan:recipe-add-record:delete']"-->
        <!--    >-->
        <!--      删除-->
        <!--    </el-button>-->
        <!--  </template>-->
        <!--</el-table-column>-->
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
  <!-- 表单弹窗：添加/修改 -->
  <RecipeAddRecordForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { RecipeAddRecordApi, RecipeAddRecordVO } from '@/api/pplan/recipeaddrecord'

/** 生产计划-加餐记录 列表 */
defineOptions({ name: 'RecipeAddRecord' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const list = ref<RecipeAddRecordVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  flightNo: undefined,
  recordDate: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RecipeAddRecordApi.getRecipeAddRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RecipeAddRecordApi.deleteRecipeAddRecord(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RecipeAddRecordApi.exportRecipeAddRecord(queryParams)
    download.excel(data, '生产计划-加餐记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 打开弹窗 */
const openAdd = async (flightNo?: string, recordDate?: string) => {
  dialogVisible.value = true
  dialogTitle.value = '加餐记录'

  // 修改时，设置数据
  if (flightNo) {
      queryParams.flightNo = flightNo
      queryParams.recordDate = recordDate
      getList()
  }
}
defineExpose({ openAdd }) // 提供 open 方法，用于打开弹窗

// /** 初始化 **/
// onMounted(() => {
//   getList()
// })
</script>
